//
//  AddInfoViewController.m
//  collageForZhang
//
//  Created by lht on 16-5-31.
//  Copyright (c) 2016年 zhshy. All rights reserved.
//

#import "AddTeacherInfoViewController.h"
#import "Teacher.h"
@interface AddTeacherInfoViewController ()
{
    AppDelegate<UIApplicationDelegate> *appDelegate;
}
@end
@implementation AddTeacherInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    [_updateBtn.layer setCornerRadius:5.0];
    [_delBtn.layer setCornerRadius:5.0];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
//单击按钮后执行数据保存操作
- (IBAction)addToDB:(id)sender {
    
//    Teacher *newManagedObject = (Teacher*)[NSEntityDescription insertNewObjectForEntityForName:@"Teacher" inManagedObjectContext:self.myDelegate.managedObjectContext];
//    [newManagedObject setTeacher_id:1];
//    [newManagedObject setTeacher_name:@"ABC"];
//    NSError *error;
//    if (![self.myDelegate.managedObjectContext save:&error]){
//        NSLog (@"The error is: %@", [error userInfo]);
//    }
//    else
//    {
//        NSLog (@"The newManagedObject's id is: %@", [newManagedObject valueForKey:@"id"]);
//    }
    

    
    
    

    //让CoreData在上下文中创建一个新对象(托管对象)
    Teacher *entry = [NSEntityDescription insertNewObjectForEntityForName:@"Teacher" inManagedObjectContext:[appDelegate managedObjectContext]];
    
    [entry setTeacher_id:[NSNumber numberWithInt:1]];
    [entry setTeacher_name:@"ABC"];
    
    NSError *error;
    
    //托管对象准备好后，调用托管对象上下文的save方法将数据写入数据库
    BOOL isSaveSuccess = [appDelegate.managedObjectContext save:&error];
    
    if (!isSaveSuccess) {
        NSLog(@"Error: %@,%@",error,[error userInfo]);
    }else {
        NSLog(@"Save successful!");
    }
}

//单击按钮后执行查询操作
- (IBAction)queryFromDB:(id)sender {
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Teacher" inManagedObjectContext:[appDelegate managedObjectContext]];
    NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"teacher_id" ascending:YES];
    NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"teacher_name" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, sortDescriptor2, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];
    [fetchRequest setEntity:entity];
    [fetchRequest setFetchBatchSize:20];
    NSArray *objects = [appDelegate.managedObjectContext executeFetchRequest:fetchRequest error:nil];
    if (objects == nil)
    {
        NSLog(@"There was an error!");
    }
    else {
        for (NSManagedObject *oneObject in objects)
        {
            NSLog(@"%@",[oneObject valueForKey:@"teacher_name"]);
        }         
    }
    

    
    
//    //创建取回数据请求
//    NSFetchRequest *request = [[NSFetchRequest alloc] init];
//    //设置要检索哪种类型的实体对象
//    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Teacher" inManagedObjectContext:self.myDelegate.managedObjectContext];
//    //设置请求实体
//    [request setEntity:entity];
//    //指定对结果的排序方式
//    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creationDate"ascending:NO];
//    NSArray *sortDescriptions = [[NSArray alloc]initWithObjects:sortDescriptor, nil];
//    [request setSortDescriptors:sortDescriptions];
//
//    
//    NSError *error = nil;
//    //执行获取数据请求，返回数组
//    NSArray *mutableFetchResult = [self.myDelegate.managedObjectContext executeFetchRequest:request error:&error] ;
//    if (mutableFetchResult == nil) {
//        NSLog(@"Error: %@,%@",error,[error userInfo]);
//    }
//    NSArray* entries = mutableFetchResult;
//    
//    NSLog(@"The count of entry:%d",[entries count]);
//    
//    for (Teacher *entry in entries) {
//        NSLog(@"Title:%d---Content:%@",entry.teacher_id,entry.teacher_name);
//    }
}

////更新操作
//-(void)updateEntry:(TEACHER *)entry
//{
//    [entry setTitle:self.titleTextField.text];
//    [entry setBody:self.contentTextField.text];
//    [entry setCreationDate:[NSDatedate]];
//    
//    NSError *error;
//    BOOL isUpdateSuccess = [self.myDelegate.managedObjectContextsave:&error ];
//    if (!isUpdateSuccess) {
//        NSLog(@"Error:%@,%@",error,[error userInfo]);
//    }
//}
//
////删除操作
//-(void)deleteEntry:(Entry *)entry
//{
//    [self.myDelegate.managedObjectContext deleteObject:entry];
//    [self.entriesremoveObject:entry];
//    
//    NSError *error;
//    if (![self.myDelegate.managedObjectContext save:&error]) {
//        NSLog(@"Error:%@,%@",error,[error userInfo]);
//    }
//}
@end
